package edu.neusoft.yuejian_09.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.activity.AddShowActivity;
import edu.neusoft.yuejian_09.adapter.ClubShowsAdapter;
import edu.neusoft.yuejian_09.adapter.MyShowsAdapter;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.adapter.ShowListAdapter;
import edu.neusoft.yuejian_09.bean.Show;
import edu.neusoft.yuejian_09.model.ShowModel;

public class ShowFragment extends Fragment implements RetrofitListener<List<Show>> {
    private RecyclerView recyclerView;
    private List mDataList;
    private View view=null;

    private TextView title;
    private ImageView img_camera;
    private PopupWindow popup;
    private int user_id;
    private int club_id;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view= inflater.inflate(R.layout.tab04_show, container, false);
        initViews();
        getshowlist();
        initEvents();
        initPopupWindow();
        return view;
    }

    //初始化控件
    private void initViews(){

        recyclerView= (RecyclerView)view.findViewById(R.id.rv_showlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        title=(TextView) view.findViewById(R.id.title);
        img_camera=(ImageView) view.findViewById(R.id.camera);

    }
    //获取晒图列表
    void getshowlist(){
        ShowModel showModel=new ShowModel();
        showModel.showlist(this);
    }
    void getshowsByUser(){
        ShowModel showModel=new ShowModel();
        showModel.getShowByUser(user_id,this);
    }

    void getshowsByClub(){
        ShowModel showModel=new ShowModel();
        showModel.getShowByClub(club_id,this);
    }
    @Override
    public void onSuccess(List<Show> data,int flag ){
        if(0==flag) {
            recyclerView.setAdapter(new ShowListAdapter(ShowFragment.this.getActivity(), data));
        }
        else if(1==flag) {
            //System.out.printf("data");
            recyclerView.setAdapter(new MyShowsAdapter(ShowFragment.this.getActivity(), data));
        }
        else if (2==flag)
        {
            recyclerView.setAdapter(new ClubShowsAdapter(ShowFragment.this.getActivity(),data));
        }
    }

    @Override
    public void onFail(){ Toast.makeText(getActivity(), "网络发生错误", Toast.LENGTH_SHORT).show();}

    //事务处理
    public void initEvents() {

        //点击搜索按钮弹出活动，比赛，大师选项
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                title.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        /*相对于某个控件的下方，并且在View这个布局中能找到对应的控件，如果时include的就不能找见*/
                        popup.showAsDropDown(view.findViewById(R.id.title), 180,20);
                    }
                });
            }
        });
        img_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ShowFragment.this.getActivity(), AddShowActivity.class);
                Bundle bundle  = new Bundle();
                bundle.putInt("user_id",user_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    //搜索弹窗相关
    private void initPopupWindow() {
        View v = getActivity().getLayoutInflater().inflate(
                R.layout.pop_show, null);


        popup = new PopupWindow(v,300, 330, true);


        popup.setFocusable(true);
        //该属性设置为true则你在点击屏幕的空白位置也会退出
        popup.setTouchable(true);

        popup.setOutsideTouchable(true);
        popup.setAnimationStyle(R.style.PopupAnimation);
        popup.setBackgroundDrawable(new ColorDrawable(0x8f000000));
        handleLogic(v);
    }

    /** 处理弹出显示内容、点击事件等逻辑*/
    private void handleLogic(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popup != null) {
                    popup.dismiss();
                }
                String showContent = "";
                switch (v.getId()) {
                    case R.id.myshows:
                        showContent = "我的晒图";
                        break;
                    case R.id.club1show:
                        showContent = "金牌健身";
                        break;
                    case R.id.club2show:
                        showContent = "快乐瑜伽";
                        break;
                }
                Toast.makeText(ShowFragment.this.getActivity(), showContent, Toast.LENGTH_SHORT).show();
            }
        };
        contentView.findViewById(R.id.myshows).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                user_id = sp.getInt("user_id",0);
                getshowsByUser();

            }
        });
        contentView.findViewById(R.id.club1show).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                club_id=1;
                getshowsByClub();
            }
        });
        contentView.findViewById(R.id.club2show).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                club_id=2;
                getshowsByClub();
            }
        });
    }


}
